#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include<time.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <stdbool.h>
#include <limits.h>
#include <stdarg.h>
#include <syslog.h>
#include "all_pub.h"
#define MAX_PATH_LEN (256) 
#define THREADS_COUNT 1
#define THREADS_BUFF_SIZE 1*1024
int main()
{
   struct stat sbuf;
   char sdirect[1000],tdirect[1000];
   char choose[1000];
   char temp1[1000],temp2[1000];
   DIR *dir_s,*dir_t;
   printf("欢迎使用本系统\t\t\t\n");
   printf("请输入A的路径:");
   scanf("%s",sdirect);
   dir_s=opendir(sdirect);
   if(dir_s==NULL)
   {
      printf("This directory don't exist !\n");
      return 0;
   }
   if(stat(sdirect,&sbuf)!=0)
   {
      printf("Get status error !\n");
      return 0;
   }
   printf("请输入B的路径:");
   scanf("%s",tdirect);
   dir_t=opendir(tdirect);
   if(dir_t==NULL)
   {
      mkdir(tdirect,sbuf.st_mode);
      chown(tdirect,sbuf.st_uid,sbuf.st_gid);
      dir_t=opendir(tdirect);
   }
   else
   {
      chmod(tdirect,sbuf.st_mode);
      chown(tdirect,sbuf.st_uid,sbuf.st_gid);
   }
   printf("1:备份A到B\t\t\n");
   printf("2:将B同步到A\t\t\n");
   printf("3:将A同步到B\t\t\n");
   printf("4:A,B同时同步\t\t\n");
   printf("5:退出系统\t\t\n");
   printf("请输入的你的选择\t\t\n");
   scanf("%s",choose);
  	 if(strcmp(choose,"1")==0)
   	{
   	printf("开始备份........\n");
	strcpy(temp1,sdirect);
	strcpy(temp2,tdirect);
	trave_dira(temp1,temp2);
	trave_diraz(temp2,temp1);
  	ProgressBar();
   	printf("备份完成\n");
	LOG(choose);
	}
   	if(strcmp(choose,"2")==0)
   	{
  	printf("开始同步........\n");
   	trave_dira(tdirect,sdirect);
	trave_diraz(sdirect,tdirect);
	ProgressBar();
  	printf("同步完成!\n");
	LOG(choose);
	}
	if(strcmp(choose,"3")==0)
	{
  	printf("开始同步........\n");
  	trave_dira(sdirect,tdirect);
	trave_diraz(tdirect,sdirect);
	ProgressBar();
  	printf("同步完成!\n");
	LOG(choose);
	}
	if(strcmp(choose,"4")==0)
	{
	trave_dir(sdirect,tdirect);
    	trave_dir(tdirect,sdirect);
    	ProgressBar();
	LOG(choose);
	} 
	if(strcmp(choose,"5")==0)
	{
		closedir(dir_t);
   		closedir(dir_s);
		printf("感谢使用本系统！\n");
		LOG(choose);
		exit(-1);
	}
	return 0;
} 

